<template>
  <div class="storage-record-list">
    <el-card class="list-card">
      <template #header>
        <div class="card-header">
          <span>仓储记录管理</span>
        </div>
      </template>
      
      <el-table
        :data="records"
        style="width: 100%"
        v-loading="loading"
      >
        <el-table-column label="序号" width="80">
          <template #default="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="货物ID" prop="cargoId" width="80" />
        <el-table-column label="区域ID" prop="areaId" width="80" />
        <el-table-column label="入库时间">
          <template #default="scope">
            {{ formatDate(scope.row.entryTime) }}
          </template>
        </el-table-column>
        <el-table-column label="预计离库时间">
          <template #default="scope">
            {{ formatDate(scope.row.expectedExitTime) }}
          </template>
        </el-table-column>
        <el-table-column label="实际离库时间">
          <template #default="scope">
            {{ formatDate(scope.row.actualExitTime) || '未离库' }}
          </template>
        </el-table-column>
        <el-table-column label="存储状态">
          <template #default="scope">
            <el-tag :type="!scope.row.actualExitTime ? 'success' : 'info'">
              {{ !scope.row.actualExitTime ? '在库' : '已出库' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button type="primary" link @click="handleView(scope.row)">查看</el-button>
            <el-button
              type="danger"
              link
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from 'axios'

export default {
  name: 'StorageRecordList',
  setup() {
    const store = useStore()
    const router = useRouter()
    const records = ref([])
    const cargos = ref([])
    const portAreas = ref([])
    const loading = ref(false)
    const currentRecord = ref(null)
    
    const fetchRecords = async () => {
      loading.value = true
      try {
        await store.dispatch('fetchStorageRecords')
        records.value = store.getters.storageRecords
        console.log('获取到的仓储记录数据:', records.value)
      } catch (error) {
        console.error('获取仓储记录失败:', error)
        ElMessage.error('获取仓储记录失败')
      } finally {
        loading.value = false
      }
    }
    
    // 获取货物列表
    const fetchCargos = async () => {
      try {
        await store.dispatch('fetchCargos')
        cargos.value = store.getters.cargos
      } catch (error) {
        console.error('获取货物列表失败:', error)
      }
    }
    
    // 获取港口区域列表
    const fetchPortAreas = async () => {
      try {
        await store.dispatch('fetchPortAreas')
        portAreas.value = store.getters.portAreas
      } catch (error) {
        console.error('获取港口区域失败:', error)
      }
    }
    
    const formatDate = (dateString) => {
      if (!dateString) return ''
      return dateString
    }
    
    const handleView = (record) => {
      ElMessage(`查看记录ID: ${record.recordId}，货物ID: ${record.cargoId}`)
    }
    
    const handleDelete = (record) => {
      ElMessageBox.confirm(
        `确定要删除记录ID为 ${record.recordId} 的仓储记录吗？`,
        '删除确认',
        {
          confirmButtonText: '删除',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).then(async () => {
        try {
          const response = await axios.delete(`storage-records/${record.recordId}`)
          if (response.data.code === 200) {
            ElMessage.success('删除仓储记录成功')
            fetchRecords()
          } else {
            ElMessage.error(response.data.message || '删除仓储记录失败')
          }
        } catch (error) {
          console.error('删除仓储记录失败:', error)
          ElMessage.error('删除仓储记录失败')
        }
      }).catch(() => {
        // 取消删除
      })
    }
    
    onMounted(() => {
      fetchRecords()
      fetchCargos()
      fetchPortAreas()
    })
    
    return {
      records,
      cargos,
      portAreas,
      loading,
      currentRecord,
      handleView,
      handleDelete,
      formatDate
    }
  }
}
</script>

<style scoped>
.storage-record-list {
  padding: 20px;
}

.list-card {
  width: 100%;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>